class Solution {
public:
    unsigned long long ans = 0;
    int widthOfBinaryTree(TreeNode* root) {
        if(!root->left&&!root->right)return 1;
        queue<pair<TreeNode*,unsigned long long>>Q;//用队列比用数组方便
        Q.push({root,1});
        while(!Q.empty()){
             int s = Q.size();
             unsigned long long start = 0, end = 0;
              for(int k = 0; k<s; k++){
                 unsigned long long idx = Q.front().second; auto r = Q.front().first;
                   Q.pop();
                   if(k == 0)start = idx;
                   if(k == s-1)end = idx;
                   if(r->left)Q.push({r->left,idx*2});
                   if(r->right)Q.push({r->right,idx*2+1});
                 }
              ans = max(ans, end-start+1);//这里记得加一
       }
        return ans;
    }
};
